/**
 * Copyright 2009 Adam Ruggles.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.sapien.guice;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.code.sapien.util.ClassUtils;
import com.google.inject.AbstractModule;
import com.google.inject.Module;
import com.google.inject.name.Names;

/**
 * Application Configuration Module for Google-Guice configuration.
 * @author Adam
 * @version $Id: ConfigModule.java 30 2010-01-10 19:05:55Z a.ruggles $
 * 
 * Created on Feb 5, 2009 at 9:26:20 PM 
 */
public class ConfigModule extends AbstractModule {
    /**
     * The <code>Logger</code> is used by the application to generate a log messages.
     */
    private static final Logger LOG = LoggerFactory.getLogger(ConfigModule.class);

    /**
     * The path of the settings file.
     */
    private final String settings;

    /**
     * Default Constructor.
     */
    public ConfigModule() {
    	super();
    	this.settings = "";
    }

	/**
     * Constructs a Configuration Module and sets the settings file.
     * @param settings The path to the configuration settings properties file.
     */
    public ConfigModule(final String settings) {
    	super();
    	this.settings = settings;
    }

    /**
	 * Adds a Guice Module by class name.
	 * @param moduleName The module's class name.
	 */
	private void addModule(final String moduleName) {
		if (StringUtils.isBlank(moduleName)) {
			LOG.debug("Cannot add a module with no name");
			return;
		}
		try {
			LOG.debug("Attempting to install module [{}]", moduleName);
			final Module module = (Module) ClassUtils.getObjectByName(moduleName);
			if (module == null) {
				LOG.error("Class not found.", moduleName);
			} else {
				install(module);
			}
		} catch (ClassCastException e) {
			LOG.error("Cannot cast [{}] to Module.", moduleName);
		}
	}

    /**
	 * {@inheritDoc}
	 * @see com.google.inject.Module#configure(com.google.inject.Binder)
	 */
	protected void configure() {
		Configuration config;
		if (StringUtils.isBlank(settings)) {
			config = new DefaultConfig();
		} else {
			config = new DefaultConfig(settings);
		}
		bind(Configuration.class).toInstance(config);
		// Bind config properties to Guice Names.
		Names.bindProperties(binder(), config.getAsProperties());
		final String modules = config.getSetting(String.class, ConfigSettings.MODULES);
		if (modules != null) {
			final String[] modulesToLoad = modules.split("\\s*,\\s*");
			for (String module : modulesToLoad) {
				addModule(module);
			}
		}
	}

	/**
	 * Returns settings.
	 * @return the settings.
	 */
	public String getSettings() {
		return settings;
	}
}
